package cn.cmdr.service.ServiceImpl;

import cn.cmdr.mapper.ReturnsMapper;
import cn.cmdr.pojo.ReturnGoods;
import cn.cmdr.service.ReturnsService;
import cn.cmdr.vo.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("returnsService")
public class ReturnsServiceImpl extends ServiceImpl<ReturnsMapper, ReturnGoods> implements ReturnsService {
    @Resource
    private ReturnsMapper returnsMapper;

    @Override
    public PageVo getPageReturnList(Integer pageNo, Integer pageSize, ReturnGoods returnGoods) {
        Page page = new Page();
        page.setSize(pageSize);
        page.setCurrent(pageNo);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(returnGoods.getId()!=null,"id",returnGoods.getId())
                .eq(returnGoods.getState()!=null,"state",returnGoods.getState())
                .like(!StringUtils.isEmpty(returnGoods.getUserCode()),"user_code",returnGoods.getUserCode())
                .eq(returnGoods.getApplicationTime()!=null,"application_time",returnGoods.getApplicationTime())
                .eq(returnGoods.getProcessingTime()!=null,"processing_time",returnGoods.getProcessingTime());
        List<ReturnGoods> list = returnsMapper.getPageReturnList(page,queryWrapper);
        return new PageVo(list,page.getTotal());
    }

    @Override
    public ReturnGoods getReturnGoodsList(Integer id) {
        return returnsMapper.getReturnGoodsList(id);
    }

    @Override
    public void getUpdate(ReturnGoods returnGoods) {
        returnsMapper.updateById(returnGoods);
    }

    @Override
    public void pdel(Integer[] id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id",id);
        returnsMapper.delete(queryWrapper);
    }

    @Override
    public void add(ReturnGoods returnGoods) {
        returnsMapper.insert(returnGoods);
    }


    @Override
    public ReturnGoods getReturnGoodsOne(Integer orderItem) {
        ReturnGoods returnGoods = returnsMapper.getReturnGoodsOne(orderItem);
        return returnGoods;
    }

    @Override
    public void getUpdateTwo(ReturnGoods returnGoods) {
        returnsMapper.getUpdateTwo(returnGoods);
    }

    @Override
    public void del(Integer orderItem) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("order_item_id",orderItem);
        returnsMapper.delete(queryWrapper);
    }

    @Override
    public Integer shifou(Integer id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("return_reason_id",id);
        return super.count(queryWrapper);
    }

    @Override
    public int getCount(Integer state) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(state!=null,"state",state);
        int count = super.count(queryWrapper);
        return count;
    }


}
